/*
 * @(#)TableConfigFilter.java   1.0  2021年11月26日
 * 
 * Copyright (c)	2014-2020. All Rights Reserved.	GuangZhou hhmk Technology Company LTD.
 */
package com.swift.dts.filter.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.swift.core.env.ConfigInterface;
import com.swift.dts.dao.TablesDao;
import com.swift.dts.filter.DtsFilter;
import com.swift.dts.loadconfig.model.DtsDataBase;
import com.swift.dts.loadconfig.model.TablesConfig;
import com.swift.dts.model.DtsBinlogMessage;
import com.swift.dts.model.Table;
import com.swift.dts.util.FormatRegexUtil;

/**
 * 添加说明 
 * @author zhengjiajin
 * @version 1.0 2021年11月26日
 */
@Component
@Order(2)
public class TableConfigFilter implements DtsFilter {

    @Autowired
    private ConfigInterface<TablesConfig> fileTablesConfig;
    
    /** 
     * @see com.swift.dts.filter.DtsFilter#filter(com.swift.dts.model.DtsBinlogMessage)
     */
    @SuppressWarnings("unchecked")
    @Override
    public boolean filter(DtsDataBase todb, DtsBinlogMessage message) {
        TablesConfig synTable = fileTablesConfig.findConfig(TablesDao.synTable);
        TablesConfig blackFilterTable = fileTablesConfig.findConfig(TablesDao.blackFilter);
        if(synTable==null) return true;
        boolean synRegex = FormatRegexUtil.isRegex(new Table(message.getDatabase(), message.getTable()), (List<String>)synTable.getRegex());
        if(!synRegex) return true;
        if(blackFilterTable==null) return false;
        boolean blackFilterRegex = FormatRegexUtil.isRegex(new Table(message.getDatabase(), message.getTable()), (List<String>)blackFilterTable.getRegex());
        if(blackFilterRegex) return true;
        return false;
    }

}
